package com.zs.zcerp.approval.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.approval.entity.EmpEntity;
import com.zs.zcerp.approval.entity.QhBasestdMEntity;
import com.zs.zcerp.approval.entity.QhBillstdD;
import com.zs.zcerp.approval.entity.QhbillstdmEntity;
import com.zs.zcerp.approval.mapper.QhBasestdMMapper;
import com.zs.zcerp.approval.service.IQhBasestdMService;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.event.UserAuthenticationUpdatedEventPublisher;
import com.zs.zcerp.common.utils.DateUtil;
import com.zs.zcerp.common.utils.FebsUtil;
import com.zs.zcerp.common.utils.SortUtil;
import com.zs.zcerp.entrance.entity.EpMain;
import com.zs.zcerp.system.service.IMenuService;
import com.zs.zcerp.system.service.IRoleService;
import com.zs.zcerp.system.service.IUserDataPermissionService;
import com.zs.zcerp.system.service.IUserRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Henry
 */

@Service
@RequiredArgsConstructor
@DS("oracle") //从oracle库数据查询
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class QhBasestdMServiceImpl extends ServiceImpl<QhBasestdMMapper, QhBasestdMEntity> implements IQhBasestdMService {

    @Override
    public IPage<QhBasestdMEntity> findBaseDetailList(QhBasestdMEntity qhBasestdMEntity, QueryRequest request) {
        if (StringUtils.isNotBlank(qhBasestdMEntity.getCreateTimeFrom()) &&
                StringUtils.equals(qhBasestdMEntity.getCreateTimeFrom(), qhBasestdMEntity.getCreateTimeTo())) {
            qhBasestdMEntity.setCreateTimeFrom(DateUtil.formatDateTime(qhBasestdMEntity.getCreateTimeFrom()));
            qhBasestdMEntity.setCreateTimeTo(DateUtil.formatDateTime(qhBasestdMEntity.getCreateTimeTo()));
        }
        Page<QhBasestdMEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countBaseDetail(qhBasestdMEntity));
        SortUtil.handlePageSort(request, page, "lastDate", FebsConstant.ORDER_DESC, false);
        return baseMapper.findBaseDetailPage(page, qhBasestdMEntity);
    }



    @Override
    public QhBasestdMEntity findBaseDetailList(String basestdNo) {
        QhBasestdMEntity param = new QhBasestdMEntity();
        param.setBasestdNo(basestdNo);
        List<QhBasestdMEntity> hq = baseMapper.findBaseDetail(param);
        return CollectionUtils.isNotEmpty(hq) ? hq.get(0) : null;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBase(QhBasestdMEntity qhBasestdMEntity) {
        qhBasestdMEntity.setLastDate(new Date());
        qhBasestdMEntity.setStatus(7);
        qhBasestdMEntity.setLastUser(FebsUtil.getCurrentUser().getUsername());
        qhBasestdMEntity.setGrtUser(FebsUtil.getCurrentUser().getUsername());
        qhBasestdMEntity.setGrtDept("ERP");
        save(qhBasestdMEntity);

    }

    @Override
    public void updateBase(QhBasestdMEntity qhBasestdMEntity) {
        QhBasestdMEntity entity = new QhBasestdMEntity();
        entity.setLastDate(new Date());
        entity.setNameT(qhBasestdMEntity.getNameT());
        entity.setLastUser(qhBasestdMEntity.getLastUser());
        baseMapper.update(entity, new LambdaQueryWrapper<QhBasestdMEntity>().eq(QhBasestdMEntity::getBasestdNo, qhBasestdMEntity.getBasestdNo()));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseByBasestdNo(String[] basestdNos) {
        List<String> list = Arrays.asList(basestdNos);
        baseMapper.delete(new QueryWrapper<QhBasestdMEntity>().lambda().in(QhBasestdMEntity::getBasestdNo, list));
    }

    @Override
    public QhBasestdMEntity findByBasestdNo(String basestdNo) {
        return baseMapper.findByBasestdNo(basestdNo);
    }

    @Override
    public QhBasestdMEntity selectByBasestdNo(String basestdNo) {
        return baseMapper.selectOne(new QueryWrapper<QhBasestdMEntity>().eq("BASESTD_NO",basestdNo));
    }

    @Override
    public List<EmpEntity> findByorgId(String orgId) {
        return baseMapper.findByorgId(orgId);
    }

    @Override
    public List<QhbillstdmEntity> findList() {
        return baseMapper.findList();
    }


}
